/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db.definitions;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author abracadabuda
 */
public class IndexDefinition extends AbstractDefinition {

    private String name;
    private String expression;
    private boolean unique;
    private boolean pk;
    private String type;
    private TableDefinition table;
    private ArrayList<String> columns = new ArrayList<String>();

    public IndexDefinition(TableDefinition table) {
        this.table = table;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getExpression() {
        return expression;
    }

    public void setExpression(String expression) {
        this.expression = expression;
    }

    public boolean isUnique() {
        return unique;
    }

    public void setUnique(boolean unique) {
        this.unique = unique;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public ArrayList<String> getColumns() {
        return columns;
    }

    public void setColumns(ArrayList<String> columns) {
        this.columns = columns;
    }

    public boolean isPk() {
        return pk;
    }

    public void setPk(boolean pk) {
        this.pk = pk;
    }

    /**
     * adds the column into columnList
     * @param column column to be added
     */
    public void addColumn(String column) {
        columns.add(column);
    }

    /**
     * prints the dump of index
     */
    public void getDump() {
        System.out.println("\t\t\tIndex:");
        System.out.println("\t\t\t\tName: " + name);
        System.out.println("\t\t\t\tExpression: " + expression);
        System.out.println("\t\t\t\tUnique: " + unique);
        System.out.println("\t\t\t\tPrimary key: " + pk);
        System.out.println("\t\t\t\tType: " + type);
    }

    /**
     * gets the SQL code of index creation
     * @return SQL code
     */
    public String getSQL() {
        if (!pk) {
            String cols = new String("");
            for (Iterator i = columns.iterator(); i.hasNext();) {
                String col = (String) i.next();
                if (!cols.equals("")) {
                    cols = cols + ", ";
                }
                cols = cols + "\"" + col + "\"";
            }
            String sql = "CREATE " + (unique ? "UNIQUE " : "") + "INDEX \"" + name + "\" ON " + "\"";
            sql += table.getSchema() + "\".";
            sql += "\"" + table.getTblName() + "\" (" + cols + ")";
            return sql + ";\n";
        }
        return "";
    }

    /**
     * gets the table, the index belongs to
     * @return table, the index belongs to 
     */
    public TableDefinition getTable() {
        return table;
    }

    /**
     * if index is primary key, this is the code, that should be used with CREATE TABLE code
     * @return SQL code 
     */
    public String getPrimaryKeySQL() {
        String sql = "";
        sql = "CONSTRAINT \"" + name + "\" PRIMARY KEY (" + getPrimartyKeyList() + ")";
        return sql;
    }

    /**
     * returns comma separated string of primary keys
     * @return comma separated string of primary keys
     */
    private String getPrimartyKeyList() {
        String pkStr = "";
        for (Iterator i = columns.iterator(); i.hasNext();) {
            pkStr += "\"" + (String) i.next() + "\"";
            if (i.hasNext()) {
                pkStr += ", ";
            }
        }
        return pkStr;
    }
}
